package com.gurkensalat.ebooks.ebookmarksync.core;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;

public class EditPreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
	private ListPreference autosyncIntervalPrefs;

	private ListPreference autosyncUrlSelectionPrefs;

	private EditTextPreference autosyncUrlPrefs;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.i(Constants.LOG_TAG, "onCreate");

		addPreferencesFromResource(R.xml.preferences);

		// Get a reference to the preferences
		autosyncIntervalPrefs = (ListPreference) getPreferenceScreen().findPreference(Constants.KEY_PREF_AUTOSYNC_INTERVAL);
		autosyncUrlSelectionPrefs = (ListPreference) getPreferenceScreen().findPreference(Constants.KEY_PREF_AUTOSYNC_URL_SELECTION);
		autosyncUrlPrefs = (EditTextPreference) getPreferenceScreen().findPreference(Constants.KEY_PREF_AUTOSYNC_URL);
	}

	@Override
	protected void onPause()
	{
		super.onPause();

		// Unregister the listener whenever a key changes
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onResume()
	{
		super.onResume();

		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		populateSummaries(sharedPreferences, null);

		// Set up a listener whenever a key changes
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
	{
		Log.i(Constants.LOG_TAG, "onSharedPreferenceChanged - " + key);

		populateSummaries(sharedPreferences, key);
	}

	private void populateSummaries(SharedPreferences sharedPreferences, String key)
	{
		Log.i(Constants.LOG_TAG, "populateSummaries - " + key);

		// if (Constants.KEY_PREF_AUTOSYNC_INTERVAL.equals(key))
		String autosyncInterval = sharedPreferences.getString(Constants.KEY_PREF_AUTOSYNC_INTERVAL, "99");
		autosyncIntervalPrefs.setSummary("Recheck every " + humanReadableTime(autosyncInterval));

		// if (Constants.KEY_PREF_AUTOSYNC_URL_SELECTION.equals(key))
		String autosyncUrlSelection = sharedPreferences.getString(Constants.KEY_PREF_AUTOSYNC_URL_SELECTION, "custom");
		autosyncUrlSelectionPrefs.setSummary("Selected Server: " + autosyncUrlSelection);
		if ("custom".equals(autosyncUrlSelection))
		{
			autosyncUrlSelectionPrefs.setSummary("Selected Server: Custom");
			autosyncUrlPrefs.setEnabled(true);
		}
		else
		{
			if ("prod-appspot".equals(autosyncUrlSelection))
			{
				autosyncUrlSelectionPrefs.setSummary("Selected Server: Production on Appspot");
				autosyncUrlPrefs.setText("http://ebookmarksync.appspot.com/");
			}
			else if ("test-appspot".equals(autosyncUrlSelection))
			{
				autosyncUrlSelectionPrefs.setSummary("Selected Server: Test on Appspot");
				autosyncUrlPrefs.setText("http://1.latest.ebookmarksync.appspot.com/");
			}
			else if ("localhost".equals(autosyncUrlSelection))
			{
				autosyncUrlSelectionPrefs.setSummary("Selected Server: Development on Localhost");
				autosyncUrlPrefs.setText("http://localhost:8080/");
			}

			autosyncUrlPrefs.setEnabled(false);
		}

		// if (Constants.KEY_PREF_AUTOSYNC_URL.equals(key))
		autosyncUrlPrefs.setSummary(autosyncUrlPrefs.getText());
	}

	private String humanReadableTime(String minutes)
	{
		// TODO: Proper I18N, maybe get values from Array...
		String result = "";

		result = minutes + " minutes";

		try
		{
			int foo = Integer.parseInt(minutes);
			if (foo == 60)
			{
				result = "1 hour";
			}
			else if (foo > 60)
			{
				result = (foo / 60) + " hours";
			}
		}
		catch (NumberFormatException nfe)
		{
			Log.e(Constants.LOG_TAG, "humanReadableTime - " + minutes, nfe);
		}

		return result;
	}

}
